﻿using System.Collections.ObjectModel;
using System.Windows.Controls;
using Kindohm.KSynth.Library;
using System;

namespace Kindohm.KSynth.Droner
{
    public partial class ChannelControl : UserControl
    {
        Mixer mixer;
        public ChannelControl()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(ChannelControl_Loaded);
        }

        public ChannelControl(Mixer mixer, string name)
            : this()
        {
            this.mixer = mixer;
            this.titleBlock.Text = name;
        }

        public ISignalChainComponent FinalInput
        {
            get;
            private set;
        }

        void ChannelControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.frequencyModAmpSlider.Maximum = uint.MaxValue;
            this.panSlider.Minimum = short.MinValue;
            this.panSlider.Maximum = short.MaxValue;

            ObservableCollection<WaveForm> items = new ObservableCollection<WaveForm>(){
                WaveForm.Sine, WaveForm.Saw, WaveForm.Square, WaveForm.Triangle};

            this.waveFormList.ItemsSource = items;
            this.waveFormList.SelectedIndex = 0;

            this.waveFormList2.ItemsSource = items;
            this.waveFormList2.SelectedIndex = 0;

            if (this.mixer == null)
                return;

Oscillator.Create(120)
    .SetFrequency(this.frequencySlider, Slider.ValueProperty)
    .WaveForm(this.waveFormList, ListBox.SelectedItemProperty)
    .FrequencyModulate(this.frequencyModHzSlider, Slider.ValueProperty, this.frequencyModAmpSlider, Slider.ValueProperty)
    .FrequencyModulationWaveForm(this.waveFormList2, ListBox.SelectedItemProperty)
    .AmplitudeModulate(this.ampModHzSlider, Slider.ValueProperty)
    .Pan(this.panSlider, Slider.ValueProperty)
    .Attenuate(this.volumeSlider, Slider.ValueProperty)
    .SendToMixer(this.mixer);
            this.FinalInput = this.mixer.Inputs[this.mixer.Inputs.Count - 1];

            this.volumeSlider.Value = -15;
       }

        private void RadioButton_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.waveFormList.SelectedItem = ((RadioButton)sender).DataContext;
        }

        private void RadioButton2_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.waveFormList2.SelectedItem = ((RadioButton)sender).DataContext;
        }

        private void deleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //need to clean up chain here somehow...

            this.mixer.Inputs.Remove(this.FinalInput);
            if (this.Deleted != null)
                this.Deleted(this, EventArgs.Empty);
        }

        public event EventHandler Deleted;

    }
}
